/**
 * 必须要指定分页ul的id,且要实例化对象，假设id为 userPage,则需要如此实例化
 * userPage= fm_page({  //实例化对象与id一致
				id : 'userPage',//ul元素id
				pageNo : 0,
				pageSize : 10,
				pageCount : 100,
				pageChange : function(pageNo) {
						//分页改变函数 传入当前页
				}
			});
 * @param cnf
 * @returns
 */
function fm_page(cnf) {

	var fmpage = {
		id:'page',
		pageNo :0,// 当前页
		pageSize : 10,// 每页条数
		pageCount : 100,// 总条数
		showPageNum : 5,// 分页元素长度
		pageNum:0,// 总页数
		choseClass:'am-active',//选中样式
		pageCmp:null,
		start:cnf.pageNo,
		init : function() {
			 var it = this;
			 if (cnf) {
				 $.each(cnf, function(k, v) {
					 it[k]=v;
				 });
			 }
			this.pageNum=Math.ceil(this.pageCount/this.pageSize)-1;
			this.renderPage();
		},
		renderPage:function(start) {
			 this.start = cnf.pageNo;
			 console.log(this.start);
			 this.pageCmp=$("#"+this.id);
			 this.pageCmp.empty();
			 this.pageCmp.append('<li ><a onclick="{0}" class="">第一页</a></li>'.replace('{0}',this.id+'.toFirst()'));
			 this.pageCmp.append('<li ><a onclick="{0}" class="">上一页</a></li>'.replace('{0}',this.id+'.toPrev()'));
			 var endNum=this.pageCount/this.pageSize>(this.showPageNum+this.start)?(this.showPageNum+this.start):this.pageCount/this.pageSize;// 截止元素
			console.log(endNum);
			 for(var i=this.start;i<endNum;i++)
				 {
				    var chose = i == this.pageNo? this.choseClass : '';
				 	var li='<li class={1} ><a  onclick="{2}" class="">{0}</a></li>';
				 	this.pageCmp.append(li.replace('{0}',i+1).replace('{1}',chose).replace('{2}',this.id+'.toPage({0})'.replace('{0}',i)) );
				 }
			 
			 this.pageCmp.append('<li ><a onclick="{0}"  class="">下一页</a></li>'.replace('{0}',this.id+'.toNext()'));
			 this.pageCmp.append('<li ><a onclick="{0}" class="">最末页</a></li>'.replace('{0}',this.id+'.toLast()'));
		},
		toNext:function(){
			this.pageNo = this.pageNo==this.pageNum?this.pageNo:this.pageNo+1;
			if(this.pageNo>=this.start+this.showPageNum&&this.pageNo<(this.pageNum)){
				this.start=this.start+this.showPageNum;
				this.renderPage(this.start);
			}
				
			if(this.pageNo<=(this.pageNum))
				this.liCls();
		},
		toPrev:function(){
			this.pageNo = this.pageNo==0?0:this.pageNo-1;
			if(this.pageNo<(this.start)){
				this.start=this.start-this.showPageNum;
				this.renderPage(this.start);
			}
			if(this.pageNo>=0)
				this.liCls();
		},
		toFirst:function(){
			this.pageNo=0;
			if(this.pageNo<(this.start)){
				this.start=0;
				this.renderPage(this.start);
			}
			this.liCls();
		},
		toLast:function(){
			this.pageNo=this.pageNum;
			if(this.pageNo>=this.start+this.showPageNum){
				this.start=this.pageNum-this.showPageNum+1;
				this.renderPage(this.start);
			}
			this.liCls();
		},
		toPage:function(pageNo){
			this.pageNo=pageNo;
			this.liCls();
		},
		liCls : function() {
			this.pageChange(this.pageNo);
			this.pageCmp.find('li:eq(' + (this.pageNo - this.start + 2) + ')')
						.siblings('li').removeClass(this.choseClass).end()
						.addClass(this.choseClass);
		},
		pageChange:function(pageNo){
			
		}
	}
	fmpage.init();
	return fmpage;
}